package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.service.RoleMenuService;
import com.itheima.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Role;
import com.itheima.service.RoleUserService;
import com.itheima.support.PageResult;
import com.itheima.support.QueryPageBean;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author admin
 */
@DubboService
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMenuService roleMenuService;
    @Resource
    private RoleUserService roleUserService;

    @Override
    public PageResult findAllRoleByPage(QueryPageBean queryPageBean) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if (queryString != null && !"".equals(queryString.trim())) {
            queryWrapper.and(roleQueryWrapper -> {
                roleQueryWrapper.like("role_name",queryString);
            });
        }
        queryWrapper.groupBy("id");
        IPage<Role> page = this.page(new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    @Transactional
    public Boolean deleteRoleById(Long id) {
        //根据id删除roleMenu表信息
        roleMenuService.deleteRoleUserByRoleId(id);
        //根据id删除roleUser表信息
        roleUserService.deleteRoleUserByRoleId(id);
        //根据id删除角色表
        this.removeById(id);
        return true;
    }



}